﻿using Company.Core.Attributes;
using Company.Core.Extensions;
using Company.Core.Helpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Core.Config
{
    /// <summary>
    /// 配置管理类实现类（配置文件管理实现类）
    /// </summary>
    [ExposedService(interfaces: typeof(IConfigManager))]
    class ConfigManager : IConfigManager
    {
        private const string _root = "Config"; // 配置文件根目录
        public ConfigManager()
        {
            if (!Directory.Exists(_root))
                Directory.CreateDirectory(_root);
        }

        public string GetFilePath(ValueType key)
        {
            return Path.Combine(_root, key.GetConfigFilename());
        }

        public T Read<T>(ValueType key)
        {
            var filename = GetFilePath(key);
            return JsonHelper.LoadFile<T>(filename);
        }

        public void Write<T>(ValueType key, T value)
        {
            Assert.NotNull(key);
            Assert.NotNull(value);
            var filename = GetFilePath(key);
            JsonHelper.SaveFile(filename, value, true);
        }
    }
}
